package com.xzq.daoimpl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.xzq.dao.UserDao;
import com.xzq.entity.Page;
import com.xzq.entity.User;
import com.xzq.util.HibernatePage;
@Repository
public class UserDaoImpl extends HibernatePage<User> implements UserDao {

	@Override
	public int regist(User user) {
		try {
			template.save(user);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public User login(User user) {
		User getUser = null;
		String hql = "from User u where u.name=? and u.pwd=?";
		Object[] obj = {user.getName(),user.getPwd()};
		List list = template.find(hql, obj);
		if(list.size()>0){
			getUser = (User) list.get(0);
		}
		return getUser;
	}

	@Override
	public int modifyUser(User user) {
		try {
			template.update(user);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int modifyUserAuthorityByAdmin(User user, int type) {
		try {
			user.getAuthority().setId(type);
			template.update(user);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Page findAllUserByAdmin(Page page) {
		String hql = "from User";
		page = findHibernatePage(page, hql, null, page.USERSIZE);
		return page;
	}

	@Override
	public User findOneUser(User user) {
		user = template.get(User.class, user.getId());
		return user;
	}

}
